package com.langgan.model_main.mvvm.viewmodel;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.langgan.common.http.NetWorkManager;
import com.langgan.model_main.ApiService;
import com.langgan.model_main.mvvm.model.DxModel;
import com.orhanobut.logger.Logger;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @brief
 * @author Beichen
 * @date 2021-12-01
 */
class MainVm extends ViewModel {
    public static MutableLiveData<DxModel> mData = new MutableLiveData<>();

    public void getData() {
        NetWorkManager.getInstance().getRetrofit()
                .create(ApiService.class)
                .getdata()
                .subscribeOn(Schedulers.io())  // 网络请求切换在io线程中调用
                .unsubscribeOn(Schedulers.io())// 取消网络请求放在io线程
                .observeOn(AndroidSchedulers.mainThread())// 观察后放在主线程调用
                .subscribe(new Observer<DxModel>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull DxModel dxModel) {
                        mData.postValue(dxModel);
                        Logger.i(dxModel.toString());
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getData2(){
        HashMap<String, String> map = new HashMap<>();
        map.put("consName","双子座");
        map.put("type","year");
        map.put("key","xxx");
        NetWorkManager.getInstance().getRetrofit()
                .create(ApiService.class)
                .getAll(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull String dxModel) {
                        Log.i("xbc", "onNext: "+dxModel.toString());
//                        mData.postValue(dxModel);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
